<!DOCTYPE HTML>
<html lang="zh" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
<input class="form-control" id="type" th:value="${type}" type="hidden"/>
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse" id="CX">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            状态：<input name="pStatus" type="text"/>
                        </li>

                        <li>
                            部门名称：<input name="deptName" type="text"/>
                        </li>

                        <li>
                            绩效方案名：<input name="projectName" type="text"/>
                        </li>
                        <li>
                            <a class="btn btn-success btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-success btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="toDetail()">
                <i class="fa fa"></i> 查看
            </a>
            <a class="btn btn-sm btn-danger btn-edit disabled" onclick="formBack()"
               shiro:hasPermission="zzjx:pefassproject:back">
                <i class="fa fa-backward"></i> 退回
            </a>
            <!--<a class="btn btn-success" onclick="doPushSign()" shiro:hasPermission="zzjx:pefctasstable:add">
                <i class="fa fa"></i> 送签
            </a>-->
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table data-mobile-responsive="true" id="bootstrap-table"></table>
        </div>
    </div>
</div>
<div class="radio-box" id="approverCodeDiv">

</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var prefix = ctx + "zzjx/pefctasstable";

    $(function () {
        $("#CX").hide();
    })

    $(function () {
        var options = {
            url: prefix + "/list",
            modalName: "绩效方案单",
            showExport: true,
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: 'ID',
                    visible: false
                },
                {
                    field: 'projectId',
                    title: '方案id',
                    visible: false
                },
                {
                    field: 'memberName',
                    title: '部门名称',
                    sortable: true
                },
                {
                    field: 'leader',
                    title: '负责人',
                    sortable: true
                },
                {
                    field: 'projectName',
                    title: '绩效方案名',
                    sortable: true
                },
                /*{
                    field : 'processType',
                    title : '当前流程',
                    sortable: true,
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel([[${@dict.getType('zzjx_pefassproject_processtype')}]], value);
                    }
                },*/
                {
                    field: 'projectStatus',
                    title: '方案状态',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel([[${@dict.getType('zzjx_pefassproject_status')}]], value);
                    }
                },
                {
                    field: 'pstatus',
                    title: '表单状态',
                    sortable: false
                },
                {
                    field: 'finalScore',
                    title: '评分',
                    sortable: true
                }]
        };
        $.table.init(options);
    });

    //表格单击行事件
    $("#bootstrap-table").on("click-row.bs.table", function (e, row, element) {
        var arrselections = $("#bootstrap-table").bootstrapTable('getSelections');
        console.log(row.id + row.pstatus);
        var type = $("#type").val();
        if (row.pstatus == "制表中" && "" == type) {
            toMakeTable(row.id);
        }
    });

    /**
     * 跳转制表页面
     */
    function toMakeTable(tableId) {
        var url = prefix + "/toMakeTable/" + tableId;
        createMenuItem(url, "制表");
    }


    /**
     * 查看详情
     */
    function toDetail(id) {
        var row = $.map($('#bootstrap-table').bootstrapTable('getSelections'), function (row) {
            return row;
        });
        var length = row.length;
        if (length != 1) {
            $.modal.alert("请选择一条数据!");
            return;
        }
        ;
        var id = row[0].id;
        var type = $("#type").val();
        if ("" == type) {
            type = 0;
        }
        var url = prefix + "/detail/" + id + "/" + type;

        //createMenuItem(url, "表单详情")
        $.modal.openFull("表单详情", url);
    }

    /**
     * 送签操作
     */
    function doPushSign() {
        var row = $.map($('#bootstrap-table').bootstrapTable('getSelections'), function (row) {
            return row;
        });
        var length = row.length;
        if (length < 1) {
            $.modal.alert("请选择一条数据!");
            return;
        }
        ;
        var idArray = new Array();
        for (var i = 0; i < length; i++) {
            if (row[i].pstatus != "制表中") {
                $.modal.alertError("所选表单包含非制表阶段的数据!");
                return false;
            }
            idArray.push(row[i].id);
        }
        var url = prefix + "/pushsign/" + idArray.join(",");
        $.ajax({
            url: url
            , type: "GET"
            , dataType: "json"
            , success: function (result) {
                console.log(result);
                $.modal.alert(result.msg);
                $.table.search();
            }
            , error: function (result) {
                console.log(result);
                $.modal.alertError(result.msg);
            }
        })
    }

    function formBack() {
        var row = $.map($('#bootstrap-table').bootstrapTable('getSelections'), function (row) {
            return row;
        });
        if (row.length != 1) {
            $.modal.alertError("请选择一条数据!");
            return false;
        }
        var tableId = row[0].id;
        if (row[0].pstatus == "制表中") {
            $.modal.alertError("表单制表中,不能退回!");
            return false;
        }
        //考评中状态为3，大于3说明已经考评完成不可退回
        /*if(parseInt(row[0].projectStatus) > 3){
            $.modal.alertError("方案已考评完成,不能退回!");
            return false;
        }*/
        var projectId = row[0].projectId;
        var url = ctx + "zzjx/pefassproject/selectProject";
        $.ajax({
            url: url,
            data: {"projectId": projectId},
            type: "GET",
            dataType: "json",
            async: false,
            success: function (result) {
                var issort = result[0].issort;
                //未启用顺序评分
                if (issort == "N") {
                    //的,初始化div
                    $.ajax({
                        url: ctx + "zzjx/pefctasstable/getApproverCodeByTableId/" + tableId,
                        type: "get",
                        dataType: "json",
                        async: false,
                        success: function (result) {
                            $("#approverCodeDiv").html("");
                            var div = "";
                            for (var i = 0; i < result.length; i++) {
                                var approverCode = result[i].approverCode;
                                var id = result[i].id;
                                div += "<input type='checkbox' name = 'approverCode'  value='" + id + "'/> <label id='approver'>" + approverCode + "</label>";
                            }
                            $("#approverCodeDiv").html(div);
                            //打开评价人列表
                            layer.open({
                                title: ['评价人', 'color:#fff;background-color:#2F4056;']
                                , skin: 'demo-class .layui-layer-title{background:#c00; color:#fff; border: none;}'
                                , type: 1
                                , area: ['400px', '300px']
                                , content: $("#approverCodeDiv")
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    //按钮【按钮一】的回调
                                    //alert(1);
                                    var userIds = [];//定义一个数组
                                    //遍历每一个名字为approverCode的复选框，其中选中的执行函数
                                    $('input[name="approverCode"]:checked').each(function () {
                                        userIds.push($(this).val());//将选中的值添加到数组chk_value中
                                    });
                                    var userId = userIds.join(",");
                                    var url = ctx + "zzjx/pefctasstable/returnBack";
                                    $.modal.loading("稍等");
                                    $.ajax({
                                        url: url,
                                        type: "POST",
                                        data: {"tableId": tableId, "projectId": projectId, "userId": userId},
                                        dataType: "json",
                                        async: false,
                                        success: function (result) {
                                            $.modal.alert(result.msg);
                                            $.table.search();
                                            $.modal.closeLoading();
                                            layer.closeAll();
                                        },
                                        error: function (result) {
                                            $.modal.alert("操作失败，请重试!");
                                            $.modal.closeLoading();
                                        }
                                    })
                                    layer.closeAll('dialog');
                                }
                                , btn2: function (index, layero) {
                                    //按钮【按钮二】的回调
                                    //return false 开启该代码可禁止点击该按钮关闭
                                    parent.layer.close(index);//关闭弹出层
                                }
                                , cancel: function () {
                                    //右上角关闭回调
                                    //return false 开启该代码可禁止点击该按钮关闭
                                    parent.layer.close(index);//关闭弹出层
                                }
                            });
                        }, error: function () {
                            $.modal.alertError("系统错误");
                        }
                    })
                } else {
                    $.modal.alertError("此表单所属方案不可退回！");
                    return false;
                }
            }, error: function () {
                $.modal.alertError("获取数据失败!");
                return false;
            }
        })
    }

    //退回操作
    /*function finalBack(){
        var row = $.map($('#bootstrap-table').bootstrapTable('getSelections'),function (row) {
            return row;
        });
        if(row.length != 1){
            $.modal.alertError("请选择一条数据!");
            return false;
        }
        var tableId = row[0].id;
        if(row[0].pstatus == "制表中"){
            $.modal.alertError("表单制表中,不能退回!");
            return false;
        }
        var url = ctx + "zzjx/pefctasstable/returnBack/"+tableId;
        $.modal.loading("稍等");
        $.ajax({
            url: url,
            type: "GET",
            dataType: "json",
            success:function (result) {
                $.modal.alert(result.msg);
                $.table.search();
                $.modal.closeLoading();
                layer.closeAll();
            },
            error:function (result) {
                $.modal.alert("操作失败，请重试!");
                $.modal.closeLoading();
            }
        })
        layer.closeAll('dialog');
    }*/
</script>
</body>
</html>